package net.hands.ado.db;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;

import net.hands.ado.db.jdbc.dialect.Dialect;
import net.hands.common.StringUtils;
import net.hands.common.ado.FilterItem;

/**
 * 说明:字段数据 <br>
 * <p>
 * 这是一个开源的软件，请在LGPLv3下合法使用、修改或重新发布。
 * </p>
 * 
 * @author 陈侃(cknet@126.com, 13910090885)
 *         <p>
 *         http://code.google.com/p/simpleframework/
 *         </p>
 *         <p>
 *         http://www.simpleframework.net
 *         </p>
 */
public class DBColumn implements Serializable {

	public static final int DEFAULT_LENGTH = 255;
	public static final int DEFAULT_PRECISION = 19;
	public static final int DEFAULT_SCALE = 2;
	public static final String YES = "YES";
	public static final String NO = "NO";
	/**
	 * 长度
	 */
	private int columnSize = DEFAULT_LENGTH;
	/**
	 * 精度
	 */
	private int precision = DEFAULT_PRECISION;

	/**
	 * 允许为空
	 */
	private String isNullable = YES;
	/**
	 * 比例
	 */
	private int scale = DEFAULT_SCALE;
	/**
	 * 内置字段排序对象
	 */
	// public static DBColumn ORDER = new DBColumn("oorder", EOrder.desc);
	/**
	 * 列名称及别名
	 */
	private String name, sqlName;

	/**
	 * 列的显示名称
	 */
	private String text;
	/**
	 * 列的数据类型
	 */
	// private Class<?> propertyClass;

	/**
	 * java.sql.Types字段类型
	 */
	private int typeCode = Types.VARCHAR;
	/**
	 * 默认值
	 */
	private String defaultValue;

	private boolean isKey = false;

	/**
	 * 过滤项
	 */
	public Collection<FilterItem> filterItems;

	/**
	 * 
	 * Constructor Method
	 * 
	 * @update 2012-12-5 下午11:07:47
	 * @param name
	 *            字段名
	 * @param text
	 *            字段值
	 */
	public DBColumn(final String name, final String text) {
		// this(name, text, null);
		this.name = name;
		this.text = text;
	}

	/**
	 * Constructor Method
	 * 
	 * @update 2012-8-10 上午10:06:03
	 * @param name
	 *            字段名
	 */
	public DBColumn(final String name) {
		this(name, name);
	}

	/**
	 * 表字段转 字段对象
	 * 
	 * @param rs
	 * @throws SQLException
	 */
	public DBColumn(final ResultSet rs) throws SQLException {
		name = rs.getString("COLUMN_NAME");
		columnSize = rs.getInt("COLUMN_SIZE");
		// decimalDigits = rs.getInt("DECIMAL_DIGITS");
		isNullable = rs.getString("IS_NULLABLE");
		typeCode = rs.getInt("DATA_TYPE");
		// typeName = new StringTokenizer( rs.getString("TYPE_NAME"), "() "
		// ).nextToken();
	}

	/**
	 * Constructor Method
	 * 
	 * @update 2012-9-13 下午12:48:54
	 * @param name
	 *            字段名
	 * @param ext
	 *            字段内容
	 * @param propertyClass
	 *            属性类
	 */
	// public DBColumn(final String name, final String ext,
	// final Class<?> propertyClass) {
	// this.name = name;
	// this.text = ext;
	// this.propertyClass = propertyClass;
	// }

	public int getTypeCode() {
		return typeCode;
	}

	public String getSqlType(Dialect dialect) throws DBException {
		return dialect.getTypeName(getTypeCode(), getLength(), getPrecision(),
				getScale());
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public void setType(int type) {
		this.typeCode = type;
	}

	public boolean isKey() {
		return isKey;
	}

	public void setKey(boolean isKey) {
		this.isKey = isKey;
	}

	public int getLength() {
		return columnSize;
	}

	public void setLength(int length) {
		this.columnSize = length;
	}

	public int getPrecision() {
		return precision;
	}

	public void setPrecision(int precision) {
		this.precision = precision;
	}

	public int getScale() {
		return scale;
	}

	public void setScale(int scale) {
		this.scale = scale;
	}

	public String getIsNullable() {
		return isNullable;
	}

	public void setIsNullable(String isNullable) {
		this.isNullable = isNullable;
	}

	/**
	 * 说明:获得字段名
	 * 
	 * @return String
	 * @update 2012-9-19 下午5:56:14
	 */
	public String getName() {
		return name;
	}

	/**
	 * 说明:设置字段名
	 * 
	 * @param columnName
	 *            字段名
	 * @update 2012-9-19 下午5:56:43
	 */
	public DBColumn setName(final String name) {
		this.name = name;
		return this;
	}

	/**
	 * 说明:获得字段别名
	 * 
	 * @return String
	 * @update 2012-9-19 下午5:57:05
	 */
	public String getText() {
		return StringUtils.hasText(text) ? text : getName();
	}

	/**
	 * 说明:设置字段别名
	 * 
	 * @param columnText
	 *            字段内容
	 * @update 2012-9-19 下午5:57:20
	 */
	public DBColumn setText(final String text) {
		this.text = text;
		return this;
	}

	public String getSqlName() {
		return StringUtils.hasText(sqlName) ? sqlName : getName();
	}

	public DBColumn setSqlName(final String sqlName) {
		this.sqlName = sqlName;
		return this;
	}

	/**
	 * 说明:获得过滤项集合
	 * 
	 * @return Collection<FilterItem>
	 * @update 2012-9-19 下午6:00:28
	 */
	public Collection<FilterItem> getFilterItems() {
		if (filterItems == null) {
			filterItems = new ArrayList<FilterItem>();
		}
		return filterItems;
	}

	/**
	 * 
	 (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 * @update 2012-9-19 下午6:00:51
	 */
	@Override
	public boolean equals(final Object obj) {
		if (obj == this) {
			return true;
		} else if (obj instanceof DBColumn) {
			return toString().equals(((DBColumn) obj).toString());
		} else {
			return super.equals(obj);
		}
	}

	/**
	 * 
	 (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 * @update 2012-9-19 下午6:01:00
	 */
	@Override
	public int hashCode() {
		return toString().hashCode();
	}

	@Override
	public String toString() {
		
		if (StringUtils.hasText(sqlName)) {
			return sqlName;
		}
		return name;

	}

	private static final long serialVersionUID = -2338071977267680196L;
}
